import { editApi, imgApi, publishApi } from "../api/article";
import { mapGetters, mapState, mapActions } from "vuex";
import { getToken } from "../utils/storage";
export default {
  data() {
    return {
      value: '',
      showPicker: false,
      showPicker2: false,
      checkboxGroup: [],
      uploader: [],
      formData: {
        title: "",
        content: "",
        status: 0,
        author: getToken("username"),
      },
    };
  },
  computed: {
    ...mapState("user", ["MyuserInfo"]),
    ...mapState("cate", ["cateList", "allTagList"]),
    ...mapGetters("cate", ["newCateName"]),
  },
  methods: {
    ...mapActions('cate', ['getCateList']),
    onConfirm(value) {
      // console.log(value);
      this.value = value;
      this.showPicker = false;
    },
    // 处理图片
    async afterRead(file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file);
      const {
        data: { savePath },
      } = await imgApi({ file: file.file });
      // console.log(savePath);
      this.formData.pic = savePath;
    },
    async onSubmit() {
      // console.log(values);
      this.formData.tags = this.checkboxGroup.join(",");
      this.formData.cateid = this.cateList.find(
        (item) => item.catename === this.value
      ).id;
      if (!this.formData.id) {
        try {
          await publishApi(this.formData);
          // console.log(res)
          this.$toast("发布成功");
          this.formData.title =
            this.formData.content =
            this.formData.status =
            this.value =
            "";
          this.checkboxGroup = this.uploader = [];
        } catch (err) {
          this.$toast("发布失败，请登录重试");
        }
      } else {
        const { errno } = await editApi(this.formData)
        // console.log(res);
        if (errno !== 0) return this.$toast("修改失败，请重试");
        this.$toast("修改成功");
        
      }
    },
    reset() {
      this.formData.title =
        this.formData.content =
        this.formData.status =
        this.value =
        "";
      this.checkboxGroup = this.uploader = [];
    },
  },
};